<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

use App\Models\User;
use App\Models\Product;


// 在之前的路由后面配上中间件
Route::get('/', 'PagesController@root')->name('root');

// 在之前的路由里加上一个 verify 参数
Auth::routes(['verify' => true]);

Route::get('alipay', function () {
    return app('alipay')->web([
        'out_trade_no' => time(),
        'total_amount' => '1',
        'subject'      => 'test subject - 测试',
    ]);
});

Route::get('test2', function () {
    $a = Product::query()->where('on_sale', true);

    $page = $a->clone()->paginate(10);
    $all  = $a->get();


    dd($page, $all);

});


Route::get('test', function () {

    function read_temp_api_order_info($number)
    {
        for ($i = 1; $i <= $number; $i++) {
            $temp_api_order_info = [
                'name'           => "磊丰" . $i,
                'email'          => 123456 + $i . '@qq.com',
                'password'       => bcrypt("password"),
                'remember_token' => now(),
                'created_at'     => now(),
                'updated_at'     => now(),
            ];
            yield $temp_api_order_info;
        }
    }


    //仓库库存扣除测试
    function cangku_stock($num = 100)
    {
        echo ("批量更新成功" . date('Y-m-d H:i:s')) . PHP_EOL;
        set_time_limit(0);                             //表示永久运行，这里我是测试array的时候用到的
        $order_info = read_temp_api_order_info($num);  //这里我就测试了10条数据，效果是看不出来的

        foreach ($order_info as $temp_api_order_info) {

            //处理数据
            User::query()->create($temp_api_order_info);

            // $api_ware_id = $this->o->getCangkuApiUrl() .'ware/program/addOutWare';
            // $out_wares = api_request($api_ware_id, $temp_api_order_info);
            // $temp_out_wares = json_decode($out_wares, true);
            // if ($temp_out_wares['code'] != 1) {
            //     $msg =  (isset($temp_out_wares['msg']) && $temp_out_wares['msg']) ? $temp_out_wares['msg'] : var_export($out_wares, true);
            //     throw new Exception($msg);
            // }
        }
        echo("批量更新成功" . date('Y-m-d H:i:s'));
    }

    cangku_stock(1000);
});


Route::redirect('/', '/products')->name('root');
Route::get('products', 'ProductsController@index')->name('products.index');
Route::post('payment/alipay/notify', 'PaymentController@alipayNotify')->name('payment.alipay.notify');
Route::post('payment/wechat/notify', 'PaymentController@wechatNotify')->name('payment.wechat.notify');
Route::post('payment/wechat/refund_notify', 'PaymentController@wechatRefundNotify')->name('payment.wechat.refund_notify');
// 后端回调不能放在 auth 中间件中
Route::post('installments/alipay/notify', 'InstallmentsController@alipayNotify')->name('installments.alipay.notify');
Route::post('installments/wechat/notify', 'InstallmentsController@wechatNotify')->name('installments.wechat.notify');
Route::post('installments/wechat/refund_notify', 'InstallmentsController@wechatRefundNotify')->name('installments.wechat.refund_notify');


Route::post('seckill_orders', 'OrdersController@seckill')->name('seckill_orders.store')->middleware('random_drop:80');

// auth 中间件代表需要登录，verified中间件代表需要经过邮箱验证
Route::group(['middleware' => ['auth', 'verified']], function () {
    Route::get('user_addresses', 'UserAddressesController@index')->name('user_addresses.index');
    Route::get('user_addresses/create', 'UserAddressesController@create')->name('user_addresses.create');
    Route::post('user_addresses', 'UserAddressesController@store')->name('user_addresses.store');
    Route::get('user_addresses/{user_address}', 'UserAddressesController@edit')->name('user_addresses.edit');
    Route::put('user_addresses/{user_address}', 'UserAddressesController@update')->name('user_addresses.update');
    Route::delete('user_addresses/{user_address}', 'UserAddressesController@destroy')->name('user_addresses.destroy');

    Route::post('products/{product}/favorite', 'ProductsController@favor')->name('products.favor');
    Route::delete('products/{product}/favorite', 'ProductsController@disfavor')->name('products.disfavor');
    Route::get('products/favorites', 'ProductsController@favorites')->name('products.favorites');


    Route::post('cart', 'CartController@add')->name('cart.add');
    Route::get('cart', 'CartController@index')->name('cart.index');
    Route::delete('cart/{sku}', 'CartController@remove')->name('cart.remove');

    Route::post('orders', 'OrdersController@store')->name('orders.store');
    Route::get('orders', 'OrdersController@index')->name('orders.index');
    Route::get('orders/{order}', 'OrdersController@show')->name('orders.show');
    Route::post('orders/{order}/received', 'OrdersController@received')->name('orders.received');
    Route::get('orders/{order}/review', 'OrdersController@review')->name('orders.review.show');
    Route::post('orders/{order}/review', 'OrdersController@sendReview')->name('orders.review.store');
    Route::post('orders/{order}/apply_refund', 'OrdersController@applyRefund')->name('orders.apply_refund');

    Route::post('crowdfunding_orders', 'OrdersController@crowdfunding')->name('crowdfunding_orders.store');


    Route::get('payment/{order}/alipay', 'PaymentController@payByAlipay')->name('payment.alipay');
    Route::get('payment/alipay/return', 'PaymentController@alipayReturn')->name('payment.alipay.return');
    Route::get('payment/{order}/wechat', 'PaymentController@payByWechat')->name('payment.wechat');
    Route::post('payment/{order}/installment', 'PaymentController@payByInstallment')->name('payment.installment');


    Route::get('coupon_codes/{code}', 'CouponCodesController@show')->name('coupon_codes.show');


    Route::get('installments', 'InstallmentsController@index')->name('installments.index');
    Route::get('installments/{installment}', 'InstallmentsController@show')->name('installments.show');
    Route::get('installments/{installment}/alipay', 'InstallmentsController@payByAlipay')->name('installments.alipay');
    Route::get('installments/alipay/return', 'InstallmentsController@alipayReturn')->name('installments.alipay.return');
    Route::get('installments/{installment}/wechat', 'InstallmentsController@payByWechat')->name('installments.wechat');


    Route::post('seckill_orders', 'OrdersController@seckill')->name('seckill_orders.store');



});

Route::get('products/{product}', 'ProductsController@show')->name('products.show');




